package com.lab.book.sharding.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lab.book.sharding.entity.User;
import com.lab.book.sharding.mapper.UserMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	public List<Long> insertData() throws SQLException {
		log.info("---------------------------- Insert Data ----------------------------");
		List<Long> result = new ArrayList<Long>(10);
		for (int i = 1; i <= 10; i++) {
			User user = new User();
			user.setUserId(i);
			user.setUserName("test_mybatis_" + i);
			user.setPwd("pwd_mybatis_" + i);
			userMapper.insert(user);
			result.add((long) user.getUserId());
		}
		return result;
	}
	
	public void deleteData(final List<Long> userIds) throws SQLException {
		log.info("---------------------------- Delete Data ----------------------------");
		for (Long each : userIds) {
			userMapper.delete(each);
		}
	}

	public void printData() throws SQLException {
		log.info("---------------------------- Print User Data -----------------------");
		for (Object each : userMapper.selectAll()) {
			System.out.println(each);
		}
	}
}
